package com.example.dangerouscargodriver.ui.fragment.home;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.baiju.bean.CityBean;
import com.baiju.bean.ProvinceBean;
import com.baiju.netmanager.Http.IHttpRequest;
import com.baiju.style.citylist.Toast.ToastUtils;
import com.example.dangerouscargodriver.MainActivity;
import com.example.dangerouscargodriver.controller.AuthController;
import com.example.dangerouscargodriver.controller.EnterPlatformInfoController;
import com.example.dangerouscargodriver.controller.UserInfoController;
import com.example.dangerouscargodriver.entry.BannerInfo;
import com.example.dangerouscargodriver.entry.MatchResourceSearchInfo;
import com.example.dangerouscargodriver.entry.ResourceInfo;
import com.example.dangerouscargodriver.entry.ResourceSearchInfo;
import com.example.dangerouscargodriver.entry.HomeDataNum;
import com.example.dangerouscargodriver.module.ResponseInfo;
import com.example.dangerouscargodriver.param.RemoteAPI;
import com.example.dangerouscargodriver.param.RemoteAPICmd;;
import com.example.dangerouscargodriver.ui.activity.CompanyLineListActivity;
import com.example.dangerouscargodriver.ui.activity.EnterPlatformCompanyStepOneActivity;
import com.example.dangerouscargodriver.ui.activity.EnterPlatformPersonalStepOneActivity;
import com.example.dangerouscargodriver.ui.activity.PersonalLineListActivity;
import com.example.dangerouscargodriver.ui.activity.ProvinceCityDistrictPickerActivity;
import com.example.dangerouscargodriver.ui.activity.PublishResourceStepOneActivity;
import com.example.dangerouscargodriver.ui.activity.SearchActivity;
import com.example.dangerouscargodriver.ui.fragment.AuthCheckFragment;
import com.example.dangerouscargodriver.ui.fragment.HttpRequestFragment;
import com.example.dangerouscargodriver.ui.fragment.mine.MineFragment;
import com.example.dangerouscargodriver.utils.DialogUtils;
import com.example.dangerouscargodriver.widget.BJPullToRefreshScrollView;
import com.example.dangerouscargodriver.widget.BannerView;
import com.example.dangerouscargodriver.widget.EmptyResourceInfoView;
import com.example.dangerouscargodriver.widget.LoadingDialog;
import com.example.dangerouscargodriver.widget.RequestMoreResourceInfoView;
import com.example.dangerouscargodriver.widget.ResourceInfoDividerView;
import com.example.dangerouscargodriver.widget.ResourceInfoView;
import com.example.dangerouscargodriver.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.ScrollViewWithListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import q.rorbin.badgeview.QBadgeView;

import java.lang.ref.WeakReference;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class HomeFragment extends AuthCheckFragment implements View.OnClickListener{

    private static final String TAG = "HomeFragment";

    private static final int START_ACTIVITY_FOR_LOCATION= 1;

    private TextView mCityPicker = null;
    private EditText mSearch = null;
    private View mRootView = null;
    private LinearLayout mResource = null;
    private LinearLayout mLine = null;

    private LinearLayout mLayoutContent = null;
    private LinearLayout mLayoutTop = null;
    private BJPullToRefreshScrollView mScrollView = null;
    private LinearLayout mStickView = null;
    private TextView mGotoLobby = null;
    private TextView mGotoLobbyStick = null;

    private HomeDataNum mHomeDataNum = null;

    private HomeHandler mHomeHandler = null;

    private TextView mResourceNum = null;
    private TextView mLineNum = null;
    private TextView mDealNum = null;
    private List<ResourceInfo> mResourceArrayMatch = null;
    private List<ResourceInfo> mResourceArray = null;
    private List<ResourceInfo> mResourceArrayMatchCache = null;
    private List<ResourceInfo> mResourceArrayCache = null;

    private List<String> mImageUrlList = null;

    private List<BannerInfo> mBannerInfo = null;

    private EmptyResourceInfoView mEmptyResourceInfoView = null;
    private ResourceInfoDividerView mDividerView = null;
    private RequestMoreResourceInfoView mRequestMore = null;

    private View mTvLatestResourceUnderline = null;
    private View mStickTvLatestResourceUnderline= null;

    private View mTvRecommendResourceUnderline = null;
    private View mStickTvRecommendResourceUnderline = null;

    private LinearLayout mLayoutRecommendResource = null;
    private LinearLayout mLayoutLatestResource = null;
    private LinearLayout mStickLayoutRecommendResource = null;
    private LinearLayout mStickLayoutLatestResource = null;
    private int mLoadRecommendResource = 0;
    private int mLoadLatestResource = 0;
    private int mLoadBanner = 0;

    private int mSelectResource = 0; //0：推荐货源 1：最新货源

    private CityBean mSelectCity = null;
    private ProvinceBean mSelectProvince = null;

    private int mSearchRecommendResourceNum = 0;
    private int mSearchLatestResourceNum = 0;
    private int mTotalRecommendResourceNum = 0;
    private int mTotalLatestResourceNum = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.initHttpComponent(HomeFragment.class);
        super.initAuthCheckComponent(HomeFragment.class);
        mHomeHandler = new HomeHandler(this);
        mImageUrlList = new ArrayList<>();
        mResourceArrayMatch = new ArrayList<>();
        mResourceArray = new ArrayList<>();
        mResourceArrayMatchCache = new ArrayList<>();
        mResourceArrayCache = new ArrayList<>();
    }

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_home, container, false);
        mCityPicker = mRootView.findViewById(R.id.city_picker);
        mSelectProvince = new ProvinceBean();
        mSelectProvince.setName("黑龙江省");
        mSelectCity = new CityBean();
        mSelectCity.setName("哈尔滨市");
        mCityPicker.setOnClickListener(this);
        mSearch = mRootView.findViewById(R.id.btn_search_resource);
        mSearch.setFocusable(false);
        mSearch.setOnClickListener(this);
        mResource = mRootView.findViewById(R.id.layout_public_resource);
        mResource.setOnClickListener(this);
        mLine = mRootView.findViewById(R.id.layout_public_line);
        mLine.setOnClickListener(this);
        mLayoutContent = mRootView.findViewById(R.id.layout_content);
        mLayoutTop = mRootView.findViewById(R.id.layout_content_top);
        mStickView = mRootView.findViewById(R.id.layout_stick_view);
        mScrollView = mRootView.findViewById(R.id.layout_scroll_view);

        mScrollView.setScrollViewListener(new BJPullToRefreshScrollView.ScrollViewListener() {
            @Override
            public void onScrollChanged(int x, int y, int oldx, int oldy) {
                if (y > mLayoutTop.getHeight())
                    mStickView.setVisibility(View.VISIBLE);
                else
                    mStickView.setVisibility(View.GONE);
            }
        });

        mGotoLobby = mRootView.findViewById(R.id.btn_goto_lobby);
        mGotoLobby.setOnClickListener(this);
        mGotoLobbyStick = mRootView.findViewById(R.id.btn_stick_goto_lobby);
        mGotoLobbyStick.setOnClickListener(this);

        mResourceNum = mRootView.findViewById(R.id.tv_resource_num);
        mLineNum = mRootView.findViewById(R.id.tv_line_num);
        mDealNum = mRootView.findViewById(R.id.tv_deal_num);

        mEmptyResourceInfoView = new EmptyResourceInfoView(mRootView.getContext());
        mDividerView = new ResourceInfoDividerView(mRootView.getContext());

        mTvLatestResourceUnderline = mRootView.findViewById(R.id.v_latest_resource_underline);
        mStickTvLatestResourceUnderline = mRootView.findViewById(R.id.v_stick_latest_resource_underline);

        mTvRecommendResourceUnderline = mRootView.findViewById(R.id.v_recommend_resource_underline);
        mStickTvRecommendResourceUnderline = mRootView.findViewById(R.id.v_stick_recommend_resource_underline);
        mTvRecommendResourceUnderline.setVisibility(View.VISIBLE);
        mStickTvRecommendResourceUnderline.setVisibility(View.VISIBLE);

        mLayoutRecommendResource = mRootView.findViewById(R.id.layout_recommend_resource);
        mLayoutRecommendResource.setOnClickListener(this);
        mLayoutLatestResource = mRootView.findViewById(R.id.layout_latest_resource);
        mLayoutLatestResource.setOnClickListener(this);

        mStickLayoutRecommendResource = mRootView.findViewById(R.id.layout_stick_recommend_resource);
        mStickLayoutRecommendResource.setOnClickListener(this);
        mStickLayoutLatestResource = mRootView.findViewById(R.id.layout_stick_latest_resource);
        mStickLayoutLatestResource.setOnClickListener(this);
        mRequestMore = new RequestMoreResourceInfoView(mRootView.getContext());
        mRequestMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mSelectResource == 0){
                    //推荐货源已经加载完毕
                    if(mSearchRecommendResourceNum >= mResourceArrayMatch.size()){
                        searchLatestResource(false);
                    }
                    else {
                        searchRecommendResource(false);
                    }
                }
                else{
                    searchLatestResource(false);
                }
            }
        });

        searchHomeDataInfo();

        searchBannerInfo();

        searchRecommendResource(true);

        searchLatestResource(true);

        intRefreshScrollView();

        showLoadingDialog();

        return mRootView;
    }

    private void searchHomeDataInfo() {
        sendHttpRequest(
                IHttpRequest.RequestMethod.GET_METHOD,
                RemoteAPI.REQUEST_V1_HOME_DATA_NUM,
                null,
                null,
                RemoteAPICmd.REQUEST_V1_HOME_DATA_NUM);
    }

    private void searchBannerInfo() {
        sendHttpRequest(
                IHttpRequest.RequestMethod.GET_METHOD,
                RemoteAPI.REQUEST_V1_HOME_BANNER,
                null,
                null,
                RemoteAPICmd.REQUEST_V1_HOME_BANNER);
    }

    private void searchLatestResource(boolean refresh) {
        if(refresh) {
            HashMap<String, String> resource_param = new HashMap<String, String>();
            resource_param.put("page", Integer.toString(1));
            sendHttpRequest(
                    IHttpRequest.RequestMethod.GET_METHOD,
                    RemoteAPI.REQUEST_V1_HOME_SEARCH_SOURCE_GOODS,
                    null,
                    resource_param,
                    RemoteAPICmd.REQUEST_V1_HOME_SEARCH_SOURCE_GOODS);
        }
        else{
            if(mSearchLatestResourceNum >= mTotalLatestResourceNum){
                ToastUtils.showLongToast(getContext(), "已无更多最新数据");
            }
            else {
                HashMap<String, String> resource_param = new HashMap<String, String>();
                resource_param.put("page", Integer.toString(mSearchLatestResourceNum / 10 + 1));

                sendHttpRequest(
                        IHttpRequest.RequestMethod.GET_METHOD,
                        RemoteAPI.REQUEST_V1_HOME_SEARCH_SOURCE_GOODS,
                        null,
                        resource_param,
                        RemoteAPICmd.REQUEST_V1_HOME_SEARCH_SOURCE_GOODS_PAGE);
            }
        }
    }

    private void searchRecommendResource(boolean refresh) {
        HashMap<String, String> header = new HashMap<String, String>();
        header.put("token", AuthController.Instance().getToken());

        HashMap<String, String> match_param = new HashMap<String, String>();
        match_param.put("page", Integer.toString(mResourceArrayMatch.size()));

        if(refresh) {
            sendHttpRequest(
                    IHttpRequest.RequestMethod.GET_METHOD,
                    RemoteAPI.REQUEST_V1_HOME_SOURCE_MATCH_SG,
                    header,
                    null,
                    RemoteAPICmd.REQUEST_V1_HOME_SOURCE_MATCH_SG);
        }
        else{
            if(mSearchRecommendResourceNum >= mTotalRecommendResourceNum){
                ToastUtils.showLongToast(getContext(), "已无更多推荐数据");
            }
            else {
                sendHttpRequest(
                        IHttpRequest.RequestMethod.GET_METHOD,
                        RemoteAPI.REQUEST_V1_HOME_SOURCE_MATCH_SG,
                        header,
                        null,
                        RemoteAPICmd.REQUEST_V1_HOME_SOURCE_MATCH_SG_PAGE);
            }
        }
    }

    @Override
    public void onHttpResponse(int actionId, String response) {
        try {
            JSONObject res = new JSONObject(response);
            Gson gson = new Gson();
            if(actionId == RemoteAPICmd.REQUEST_V1_HOME_DATA_NUM){
                ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
                if(responseInfo != null && responseInfo.getStatus() == 1){
                    JSONObject data = res.getJSONObject("data");
                    mHomeDataNum = gson.fromJson(data.toString(), HomeDataNum.class);

                    mResourceNum.setText(mHomeDataNum.getGrand_total_source());
                    mLineNum.setText(mHomeDataNum.getGrand_total_line());
                    mDealNum.setText(mHomeDataNum.getTrading_volume());
                }
            }
            else if(actionId == RemoteAPICmd.REQUEST_V1_HOME_SEARCH_SOURCE_GOODS){
                Log.d(TAG, "response: " + response);
                ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
                if(responseInfo != null && responseInfo.getStatus() == 1){
                    JSONObject data = res.getJSONObject("data");
                    ResourceSearchInfo searchInfo = gson.fromJson(data.toString(), ResourceSearchInfo.class);
                    mTotalLatestResourceNum = searchInfo.getCount();
                    if(mResourceArray.size() < searchInfo.getCount()){
                        mSearchLatestResourceNum += searchInfo.getSource_goods().size();
                        for(int index = 0; index < searchInfo.getSource_goods().size(); index++){
                            mResourceArray.add(searchInfo.getSource_goods().get(index));
                        }
                    }
                }
                mLoadLatestResource = 1;
                refreshResourceViewInFragment();
                refreshComplete();
                if(isLoadResourceComplete()){
                    dismissLoadingDialog();
                }
            }
            else if(actionId == RemoteAPICmd.REQUEST_V1_HOME_SEARCH_SOURCE_GOODS_PAGE){
                Log.d(TAG, "response: " + response);
                ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
                if(responseInfo != null && responseInfo.getStatus() == 1){
                    JSONObject data = res.getJSONObject("data");
                    ResourceSearchInfo searchInfo = gson.fromJson(data.toString(), ResourceSearchInfo.class);
                    if(mResourceArray.size() < searchInfo.getCount()){
                        mSearchLatestResourceNum += searchInfo.getSource_goods().size();
                        for(int index = 0; index < searchInfo.getSource_goods().size(); index++){
                            mResourceArray.add(searchInfo.getSource_goods().get(index));
                        }
                    }
                }
                refreshComplete();
                refreshResourceViewInFragment();
            }
            else if(actionId == RemoteAPICmd.REQUEST_V1_HOME_BANNER){
                ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
                if(responseInfo != null && responseInfo.getStatus() == 1){
                    JSONArray data = res.getJSONArray("data");
                    Type type = new TypeToken<List<BannerInfo>>() {}.getType();
                    mBannerInfo = gson.fromJson(data.toString(), type);
                }
            }
            else if(actionId == RemoteAPICmd.REQUEST_V1_HOME_SOURCE_MATCH_SG){
                Log.d(TAG, "REQUEST_V1_HOME_SOURCE_MATCH_SG: " + response);
                ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
                if(responseInfo != null && responseInfo.getStatus() == 1){
                    JSONObject data = res.getJSONObject("data");
                    MatchResourceSearchInfo searchInfo = gson.fromJson(data.toString(), MatchResourceSearchInfo.class);
                    mTotalRecommendResourceNum = searchInfo.getTotal();
                    if(mResourceArrayMatch.size() < mTotalRecommendResourceNum){
                        mSearchRecommendResourceNum += searchInfo.getList_push().size();
                        for(int index = 0; index < searchInfo.getList_push().size(); index++){
                            mResourceArrayMatch.add(searchInfo.getList_push().get(index));
                        }
                    }
                }
                refreshResourceViewInFragment();
                refreshComplete();
                mLoadRecommendResource = 1;
                if(isLoadResourceComplete()){
                    dismissLoadingDialog();
                }
            }
            else if(actionId == RemoteAPICmd.REQUEST_V1_HOME_SOURCE_MATCH_SG_PAGE){
                Log.d(TAG, "REQUEST_V1_HOME_SOURCE_MATCH_SG_PAGE: " + response);
                ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
                if(responseInfo != null && responseInfo.getStatus() == 1){
                    JSONObject data = res.getJSONObject("data");
                    MatchResourceSearchInfo searchInfo = gson.fromJson(data.toString(), MatchResourceSearchInfo.class);
                    if(mResourceArrayMatch.size() < searchInfo.getTotal()){
                        mSearchRecommendResourceNum += searchInfo.getList_push().size();
                        for(int index = 0; index < searchInfo.getList_push().size(); index++){
                            mResourceArrayMatch.add(searchInfo.getList_push().get(index));
                        }
                    }
                }
                refreshComplete();
                refreshResourceViewInFragment();
            }
            else{

            }
        }
        catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onHttpErrorResponse(int actionId, String response) {
        if(actionId == RemoteAPICmd.REQUEST_V1_HOME_SOURCE_MATCH_SG) {
            refreshComplete();
            mLoadRecommendResource = 1;
            if(isLoadResourceComplete()){
                dismissLoadingDialog();
            }
        }
        else if(actionId == RemoteAPICmd.REQUEST_V1_HOME_SEARCH_SOURCE_GOODS) {
            refreshComplete();
            mLoadLatestResource = 1;
            if(isLoadResourceComplete()){
                dismissLoadingDialog();
            }
        }
        else if(actionId == RemoteAPICmd.REQUEST_V1_HOME_SOURCE_MATCH_SG_PAGE ||
            actionId == RemoteAPICmd.REQUEST_V1_HOME_SEARCH_SOURCE_GOODS_PAGE){
            ToastUtils.showLongToast(getContext(), "加载数据失败");
        }
        else if(actionId == RemoteAPICmd.REQUEST_V1_HOME_BANNER){
            /**/
        }
        else{

        }
    }

    private static class HomeHandler extends Handler {
        private String TAG = "HomeHandler";
        private final WeakReference<HomeFragment> mFragment;

        HomeHandler(HomeFragment fragment) {
            mFragment = new WeakReference<HomeFragment>(fragment);
        }

        @Override
        public void handleMessage(Message msg) {
            HomeFragment fragment = mFragment.get();
            switch (msg.what){
                default:
                    break;
            }
        }
    }

    private boolean isLoadResourceComplete() {
        return mLoadLatestResource == 1 &&
                mLoadRecommendResource == 1;
    }

    synchronized private void refreshResourceViewInFragment() {
        mLayoutContent.removeAllViews();

        if(mSelectResource == 0) {
            if (mResourceArrayMatch.size() > 0) {
                for (int index = 0; index < mResourceArrayMatch.size(); index++) {
                    final int click = index;
                    if (index == 1) {
                        if (mImageUrlList.size() > 0) {
                            BannerView banner_view = new BannerView(mRootView.getContext(),
                                    mBannerInfo);
                            mLayoutContent.addView(banner_view);
                        }
                    }
                    ResourceInfoView view = new ResourceInfoView(mRootView.getContext(),
                            mResourceArrayMatch.get(index));
                    view.bindData(mResourceArrayMatch.get(index));
                    mLayoutContent.addView(view);
                }
            } else {
                mLayoutContent.addView(mEmptyResourceInfoView);
            }
            mLayoutContent.addView(mDividerView);
        }

        if(mResourceArray.size() > 0) {
            for(int index = 0; index < mResourceArray.size(); index++) {
                final int click = index;
                if(index == 1){
                    if(mImageUrlList.size() > 0) {
                        BannerView banner_view = new BannerView(mRootView.getContext(),
                                mBannerInfo);
                        mLayoutContent.addView(banner_view);
                    }
                }
                ResourceInfoView view = new ResourceInfoView(mRootView.getContext(),
                        mResourceArray.get(index));
                view.bindData(mResourceArray.get(index));
                mLayoutContent.addView(view);
            }
        }

        mLayoutContent.addView(mRequestMore);
    }

    //进行初使化  （1.设置模式 2.通过getLoadingLayoutProxy方法设置header与footer的刷新显示文字 3.设置监听事件）
    private void intRefreshScrollView() {
        //1.设置模式
        mScrollView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

        //2.通过调用getLoadingLayoutProxy方法，设置下拉刷新状况布局中显示的文字 ，第一个参数为true,代表下拉刷新
        ILoadingLayout headLables = mScrollView.getLoadingLayoutProxy(true, false);
        headLables.setPullLabel("下拉刷新");
        headLables.setRefreshingLabel("正在刷新");
        headLables.setReleaseLabel("松开刷新");
/*
        //2.设置上拉加载底部视图中显示的文字，第一个参数为false,代表上拉加载更多
        ILoadingLayout footerLables = mScrollView.getLoadingLayoutProxy(false, true);
        footerLables.setPullLabel("上拉加载");
        footerLables.setRefreshingLabel("正在载入...");
        footerLables.setReleaseLabel("松开加载更多");
*/
        //3.设置监听事件
        mScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollViewWithListener>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollViewWithListener> refreshView) {
                //请求网络数据，并更新listview组件
                if(mSelectResource == 0){
                    mResourceArrayMatch.clear();
                    mTotalRecommendResourceNum = 0;
                    mSearchRecommendResourceNum = 0;
                    searchRecommendResource(true);
                }
                else{
                    mResourceArray.clear();
                    mTotalLatestResourceNum = 0;
                    mSearchLatestResourceNum = 0;
                    searchLatestResource(true);
                }
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollViewWithListener> refreshView) {
                //请求网络数据，并更新listview组件
                refreshComplete();//数据加载完成后，关闭header,footer
            }
        });

    }
    /**
     * 刷新完成时关闭
     */
    public void refreshComplete(){
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mScrollView.onRefreshComplete();
            }
        },1000);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btn_search_resource){
            Intent intent = new Intent(this.getActivity(), SearchActivity.class);
            startActivity(intent);
        }
        else if(v.getId() == R.id.city_picker){
            Intent intent = new Intent(HomeFragment.this.getActivity(), ProvinceCityDistrictPickerActivity.class);
            intent.putExtra("type", ProvinceCityDistrictPickerActivity.AREA_SELECT_CITY);
            intent.putExtra("province", mSelectProvince);
            intent.putExtra("city", mSelectCity);
            startActivityForResult(intent, START_ACTIVITY_FOR_LOCATION);
        }
        else if(v.getId() == R.id.layout_public_resource){
            if(checkAuth()) {
                Intent intent = new Intent(this.getActivity(), PublishResourceStepOneActivity.class);
                startActivity(intent);
            }
            else{
                gotoLogInActivity();
            }
        }
        else if(v.getId() == R.id.layout_public_line){
            if(checkAuth()) {
                if(UserInfoController.Instance().getUserInfo().getRole() == 2) {
                    Intent intent = new Intent(this.getActivity(), CompanyLineListActivity.class);
                    startActivity(intent);
                }
                else if(UserInfoController.Instance().getUserInfo().getRole() == 3){
                    Intent intent = new Intent(this.getActivity(), PersonalLineListActivity.class);
                    startActivity(intent);
                }
                else{
                    DialogUtils.showAlertDialog(getActivity(),
                            getResources().getString(R.string.title_choose_enter_role),
                            getResources().getString(R.string.title_choose_enter_role_meta),
                            getResources().getString(R.string.text_enter_company),
                            getResources().getString(R.string.text_enter_personal),
                            true,
                            new DialogUtils.AlertDialogBtnClickListener() {
                                @Override
                                public void clickPositive() {
                                    //negative
                                    Intent intent = new Intent(HomeFragment.this.getActivity(), EnterPlatformCompanyStepOneActivity.class);
                                    startActivity(intent);
                                }
                                @Override
                                public void clickNegative() {
                                    //positive
                                    Intent intent = new Intent(HomeFragment.this.getActivity(), EnterPlatformPersonalStepOneActivity.class);
                                    startActivity(intent);
                                }
                            });
                }
            }
            else{
                gotoLogInActivity();
            }
        }
        else if(v.getId() == R.id.btn_stick_goto_lobby || v.getId() == R.id.btn_goto_lobby){
            Intent intent=new Intent();
            intent.setAction(MainActivity.ACTION_SWITCH_FRAGMENT);
            intent.putExtra("Goal",1);
            mRootView.getContext().sendBroadcast(intent);
        }
        else if(v.getId() == R.id.layout_recommend_resource){
            mTvRecommendResourceUnderline.setVisibility(View.VISIBLE);
            mStickTvRecommendResourceUnderline.setVisibility(View.VISIBLE);
            mTvLatestResourceUnderline.setVisibility(View.GONE);
            mStickTvLatestResourceUnderline.setVisibility(View.GONE);
            mSelectResource = 0;
            refreshResourceViewInFragment();
        }
        else if(v.getId() == R.id.layout_stick_recommend_resource){
            if(mSelectResource == 1){
                mTvRecommendResourceUnderline.setVisibility(View.VISIBLE);
                mStickTvRecommendResourceUnderline.setVisibility(View.VISIBLE);
                mTvLatestResourceUnderline.setVisibility(View.GONE);
                mStickTvLatestResourceUnderline.setVisibility(View.GONE);
                mSelectResource = 0;

                if(mResourceArrayMatch.size() > 0){
                    for(int index = 0; index < mResourceArrayMatch.size(); index++) {
                        ResourceInfoView view = new ResourceInfoView(mRootView.getContext(),
                                mResourceArrayMatch.get(index));
                        view.bindData(mResourceArrayMatch.get(index));
                        mLayoutContent.addView(view, 0);
                    }

                    if(mImageUrlList.size() > 0) {
                        BannerView banner_view = new BannerView(mRootView.getContext(), mBannerInfo);
                        mLayoutContent.addView(banner_view, 1);
                        mLayoutContent.addView(mDividerView, mResourceArrayMatch.size() + 1);
                    }
                    else{
                        mLayoutContent.addView(mDividerView, mResourceArrayMatch.size());
                    }
                }
                else{
                    mLayoutContent.addView(mEmptyResourceInfoView, 0);
                    mLayoutContent.addView(mDividerView, 1);
                }

                mScrollView.getRefreshableView().scrollTo(0, mLayoutTop.getBottom());
            }
        }
        else if(v.getId() == R.id.layout_latest_resource){
            mTvRecommendResourceUnderline.setVisibility(View.GONE);
            mStickTvRecommendResourceUnderline.setVisibility(View.GONE);
            mTvLatestResourceUnderline.setVisibility(View.VISIBLE);
            mStickTvLatestResourceUnderline.setVisibility(View.VISIBLE);
            mSelectResource = 1;
            refreshResourceViewInFragment();
        }
        else if(v.getId() == R.id.layout_stick_latest_resource){
            if(mSelectResource == 0){
                mTvRecommendResourceUnderline.setVisibility(View.GONE);
                mStickTvRecommendResourceUnderline.setVisibility(View.GONE);
                mTvLatestResourceUnderline.setVisibility(View.VISIBLE);
                mStickTvLatestResourceUnderline.setVisibility(View.VISIBLE);
                mSelectResource = 1;

                //把推荐货源相关的信息移除
                if(mResourceArrayMatch.size() > 0){
                    for(int index = 0; index < mResourceArrayMatch.size() + 1; index++){
                        mLayoutContent.removeViewAt(0);
                    }
                }
                else{
                    for(int index = 0; index < 2; index++){
                        mLayoutContent.removeViewAt(0);
                    }
                }

                mScrollView.getRefreshableView().scrollTo(0, mLayoutTop.getBottom());
            }
        }
        else{

        }
    }

    @Override
    protected void onLogIn() {
        Log.d(TAG, "onLogIn");

        //请求网络数据，并更新listview组件
        mResourceArrayMatch.clear();
        searchRecommendResource(true);
        mResourceArray.clear();
        mTotalLatestResourceNum = 0;
        mSearchLatestResourceNum = 0;
        searchLatestResource(true);
    }

    @Override
    protected void onLogOut() {
        Log.d(TAG, "onLogOut");

        mResourceArrayMatch.clear();
        searchRecommendResource(true);

        mResourceArray.clear();
        mTotalLatestResourceNum = 0;
        mSearchLatestResourceNum = 0;
        searchLatestResource(true);
    }

    @Override
    protected void onUserChanged() {
        //请求网络数据，并更新listview组件
        mResourceArrayMatch.clear();
        searchRecommendResource(true);
        mResourceArray.clear();
        mTotalLatestResourceNum = 0;
        mSearchLatestResourceNum = 0;
        searchLatestResource(true);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == START_ACTIVITY_FOR_LOCATION){
            if(data != null) {
                mSelectCity = (CityBean) data.getParcelableExtra("city");
                mSelectProvince = (ProvinceBean) data.getParcelableExtra("province");
                if (mSelectCity != null) {
                    Log.d(TAG, "name: " + mSelectCity.getName());
                    mCityPicker.setText(mSelectCity.getName());
                }
            }
        }
    }

    @Override
    public void onPause(){
        super.onPause();
        Log.d(TAG, "onPause");
    }

    @Override
    public void onResume(){
        super.onResume();
        Log.d(TAG, "onResume");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }
    /**
     * BottomNavigationView显示角标
     *
     * @param layout layout索引
     * @param showNumber 显示的数字，小于等于0是将不显示
     */
    private void showBadgeView(int layout, int showNumber) {
        Log.d(TAG, "showBadgeView");

        // 显示badegeview
        new QBadgeView(mRootView.getContext())
                .bindTarget(mRootView.findViewById(layout))
                .setBadgeGravity(Gravity.TOP | Gravity.END)
                .setBadgeBackgroundColor(Color.parseColor("#FFD401"))
                .setBadgeTextColor(Color.BLACK)
                //.setBadgePadding(10, true)
                .setBadgeNumber(showNumber);
    }
}